
function checkValue(el) {
	var value = el.value;
	value = value.replace(/ /g,"");
    if (value=="" || isNaN(value)) {
        alert("Please enter a valid value");
        el.focus();
        return false;
    }
    el.value=value;
    return true;
}

function validate() {


    if (!inverse) {
    	if (!checkValue(document.forms["thConfig"].minLevel)) return;
    	if (!checkValue(document.forms["thConfig"].warningLevel)) return;
    	if (!checkValue(document.forms["thConfig"].criticalLevel)) return;
    	if (!checkValue(document.forms["thConfig"].maxLevel)) return;
    	
    	minLevel = parseInt(document.forms["thConfig"].minLevel.value);
        maxLevel = parseInt(document.forms["thConfig"].maxLevel.value);
        criticalLevel = parseInt(document.forms["thConfig"].criticalLevel.value);
        warningLevel = parseInt(document.forms["thConfig"].warningLevel.value);
        
	    if (minLevel<0) {
	    	alert("Minimum allowed healthy value is 0.");
	    	return;
	    }
	    if (minLevel>warningLevel) {
	    	alert("Healthy range should be in increasing order");
	    	return;
	    }
	    if (warningLevel>criticalLevel) {
	    	alert("Not so healthy value should be greater than or equal to "+warningLevel);
	    	return;
	    }
	    if (criticalLevel>maxLevel) {
	    	alert("DBA attention value should be greater than or equal to "+criticalLevel);
	    	return;
	    }
    } else {
    	if (!checkValue(document.forms["thConfig"].maxLevel)) return;
    	if (!checkValue(document.forms["thConfig"].criticalLevel)) return;
    	if (!checkValue(document.forms["thConfig"].warningLevel)) return;
    	if (!checkValue(document.forms["thConfig"].minLevel)) return;
    	
    	minLevel = parseInt(document.forms["thConfig"].minLevel.value);
        maxLevel = parseInt(document.forms["thConfig"].maxLevel.value);
        criticalLevel = parseInt(document.forms["thConfig"].criticalLevel.value);
        warningLevel = parseInt(document.forms["thConfig"].warningLevel.value);
        
	    if (maxLevel>100) {
	    	alert("Maximum allowed healthy level is 100%");
	    	return;
	    }
	    if (maxLevel<criticalLevel) {
	    	alert("Healthy range should be in decreasing order");
	    	return;
	    }
	    if (warningLevel>criticalLevel) {
	    	alert("Not so healthy value should be less than or equal to "+criticalLevel);
	    	return;
	    }
	    if (minLevel>warningLevel) {
	    	alert("DBA attention value should be less than or equal to "+warningLevel);
	    	return;
	    }
    }
	this.document.forms[0].submit();
}

function copyWarningLevel() {	
	document.forms["thConfig"].fixedWarningLevel.value = document.forms["thConfig"].warningLevel.value;	   
}

function copyCriticalLevel() {	
	document.forms["thConfig"].fixedCriticalLevel.value = document.forms["thConfig"].criticalLevel.value;	   
}